package sheetOperations;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import storage.FieldInfo;

import data.EmailReminder;
import data.ExceptionInfo;
import data.PortList;

import fileIO.CellIO;

public class WorkbookSegregation {
	/**
	 * 
	 * @param wkb List of rows to be marked
	 * @param allProfiles skip certain profiles
	 * @return
	 */
	public static HashMap<String,ArrayList<Row>> segregateWB(HSSFWorkbook wkb, String rptName, boolean checkReminder){
		//Parse each row in the book according to the rules specified in each profile
		//Contains two lists, one for dest one for origin
		HashMap<String,ArrayList<Row>> returnMap = new HashMap<String,ArrayList<Row>>();
		Row titleRow = null;
		HSSFSheet wks = wkb.getSheetAt(0);

		Iterator<Row> rowIterator = wks.iterator();
		
		boolean titleExists = false;
		if(FieldInfo.getTitleRow()>0){
			titleExists = true;
		}
		//Check all rows in workbook and mark as needed
		while(rowIterator.hasNext()){
			Row row = rowIterator.next();
			//Skip all rows before title row if title row exists
			if(titleExists && (row.getRowNum()<=FieldInfo.getTitleRow()-1)){
				titleRow = row;
				continue;
			}			
			//CHECK Exception List
			String currKey = CellIO.keyGenerator(row);	
			boolean inException = false;
			if (ExceptionInfo.containKey(currKey)){
				inException = true;
			}
			if (!inException){
				String titlePostfix = "";
				if(checkReminder && EmailReminder.containKey(currKey)){
					int reminderC = EmailReminder.getReminderCount(currKey);
					if (reminderC == 1){
						titlePostfix = "2nd Reminder";
					}else if (reminderC > 1){
						titlePostfix = "3rd Reminder";
					}
				}
				String cneeName = CellIO.ConverttoStr(row.getCell(FieldInfo.getCneeColumn()));
				//Pulling origin name from Map for title
				String originName = CellIO.ConverttoStr(row.getCell(FieldInfo.getOriginColumn()));
				String title = titlePostfix + "("+ originName + ")-" + cneeName + " " + rptName;
				ArrayList<Row>tempArrList = new ArrayList<Row>();
				if (returnMap.containsKey(title)){
					tempArrList = returnMap.get(title);
					tempArrList.add(row);
				}else{
					tempArrList.add(titleRow);
					tempArrList.add(row);
				}
				returnMap.put(title, tempArrList);
			}
		}
		return returnMap;
	}
	public static HashMap<String,ArrayList<Row>> segregateWBwPort(HSSFWorkbook wkb, String rptName, boolean checkReminder){
		//Parse each row in the book according to the rules specified in each profile
		//Contains two lists, one for dest one for origin
		HashMap<String,ArrayList<Row>> returnMap = new HashMap<String,ArrayList<Row>>();
		Row titleRow = null;
		HSSFSheet wks = wkb.getSheetAt(0);

		Iterator<Row> rowIterator = wks.iterator();
		
		boolean titleExists = false;
		if(FieldInfo.getTitleRow()>0){
			titleExists = true;
		}
		//Check all rows in workbook and mark as needed
		while(rowIterator.hasNext()){
			Row row = rowIterator.next();
			//Skip all rows before title row if title row exists
			if(titleExists && (row.getRowNum()<=FieldInfo.getTitleRow()-1)){
				titleRow = row;
				continue;
			}			
			//CHECK Exception List
			String currKey = CellIO.keyGenerator(row);	
			boolean inException = false;
			if (ExceptionInfo.containKey(currKey)){
				inException = true;
			}
			if (!inException){
				String titlePostfix = "";
				if(checkReminder && EmailReminder.containKey(currKey)){
					int reminderC = EmailReminder.getReminderCount(currKey);
					if (reminderC == 1){
						titlePostfix = "2nd Reminder";
					}else if (reminderC > 2){
						titlePostfix = "3rd Reminder";
					}
				}
				String cneeName = CellIO.ConverttoStr(row.getCell(FieldInfo.getCneeColumn()));
				//Pulling origin name from Map for title
				String originName = CellIO.ConverttoStr(row.getCell(FieldInfo.getOriginColumn()));
				if(PortList.containKey(originName))
					originName = PortList.getCode(originName);
				else
					originName = "DEFAULT";
				String title = titlePostfix + "("+ originName + ")-" + cneeName + " " + rptName;
				ArrayList<Row>tempArrList = new ArrayList<Row>();
				if (returnMap.containsKey(title)){
					tempArrList = returnMap.get(title);
					tempArrList.add(row);
				}else{
					tempArrList.add(titleRow);
					tempArrList.add(row);
				}
				returnMap.put(title, tempArrList);
			}
		}
		return returnMap;
	}
	public static int boolToInt(boolean bool){
		if(bool)
			return 1;
		else
			return 0;
	}
	public static boolean intToBool(int num){
		return(num!=0);
	}
	/**
	 * Checks if one calendar is after another by a certain number of days
	 * @param b
	 * @param a
	 * @param days
	 * @return
	 */
}
